package com.fz.song.service.impl;

import com.fz.song.config.BlogConfig;
import com.fz.song.dao.BlogAdminMapper;
import com.fz.song.pojo.BlogAdmin;
import com.fz.song.service.IBlogAdminService;
import com.fz.song.util.ResultObj;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class BlogAdminServiceImpl implements IBlogAdminService {
    private static Log logger = LogFactory.getLog(BlogAdminServiceImpl.class);
    @Autowired
    private BlogAdminMapper blogAdminMapper;

    public ResultObj login(HttpServletRequest request,BlogAdmin blogAdmin ) {
        if (null == blogAdmin.getAccount() || "".equals(blogAdmin.getAccount())) {
            logger.info("博客登陆用户名为空");
            return ResultObj.error("登陆失败");
        }
        if (null == blogAdmin.getPassword() || "".equals(blogAdmin.getPassword())) {
            logger.info("博客登陆密码为空");
            return ResultObj.error("登陆失败");
        }
        List<BlogAdmin> list = blogAdminMapper.getAdminByUsernameAndPwd(blogAdmin);
        if (list != null && list.size() > 0) {
            BlogAdmin blogAdmin_db = list.get(0);
            HttpSession session = request.getSession();
            session.setAttribute(BlogConfig.ADMIN_LOGIN, blogAdmin_db);
            return ResultObj.ok("登陆成功");
        }
        return ResultObj.error("登陆失败");
    }
}
